//
//  ZYIndexDeviceView.m
//  YiLin
//
//  Created by Zyark on 2018/6/8.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYIndexDeviceView.h"
#import "ZYIndexDeviceItemView.h"
#import "ZYDevice.h"

@interface ZYIndexDeviceView()<ZYIndexDeviceItemViewDelegate>
//@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,strong) UILabel *roomTypeLabel;
@property(nonatomic,strong) NSArray *deviceArray;
@property(nonatomic,strong) NSMutableArray *deviceItemViewArray;
@end

@implementation ZYIndexDeviceView
-(instancetype)initWithFrame:(CGRect)frame withRoomType:(ZYHomeRoomType)type withDeviceArray:(NSArray *)deviceArray{
    self = [super initWithFrame:frame];
    if (self) {
        _roomType = type;
        _deviceArray = deviceArray;
        [self setup];
    }
    return self;
}

-(void)setup{
    CGFloat itemHeight = 80;
    CGFloat roomTypeLabelHeight = 20;
//    self.height = [_deviceArray count] * (itemHeight + 10) + roomTypeLabelHeight + 20 ;
//    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
//    _bgView.backgroundColor = [UIColor clearColor];
//    [self addSubview:_bgView];
//    _bgView.centerX = self.centerX;
//    _bgView.centerY = self.centerY;
    
    _roomTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, self.width, roomTypeLabelHeight)];
    _roomTypeLabel.textAlignment = NSTextAlignmentCenter;
    _roomTypeLabel.textColor = [UIColor whiteColor];
    [self addSubview:_roomTypeLabel];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height - _roomTypeLabel.bottom)];
//    _scrollView.alwaysBounceVertical = YES;
    _scrollView.contentSize = CGSizeMake(0, [_deviceArray count] * (itemHeight + 10) + roomTypeLabelHeight + 40);
    if (@available(iOS 11.0, *)) {
        _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_scrollView];
    _scrollView.top = _roomTypeLabel.bottom + 20;
    
    _deviceItemViewArray = [NSMutableArray arrayWithCapacity:_deviceArray.count];
    int count = 0;
    for (ZYDevice *device in _deviceArray) {

        ZYIndexDeviceItemView *itemView = [[ZYIndexDeviceItemView alloc] initWithFrame:CGRectMake(0, 0, self.width - 30, itemHeight) withDeviceType:device.type];
        itemView.layer.cornerRadius = 5;
        itemView.layer.masksToBounds = YES;
        itemView.device = device;
        [_scrollView addSubview:itemView];
        itemView.top = count * (itemHeight + 10);
        itemView.centerX = self.width / 2;
        itemView.delegate = self;
        [_deviceItemViewArray addObject:itemView];
        count ++;
    }
    self.backgroundColor = [UIColor clearColor];
    [self setupTitle];
}

-(void)setupTitle{
    NSString *title = LC_INDEX_MAIN_DEVICE_LABEL;
//    switch (_roomType) {
//        case ZYHomeRoomTypeBedroom:
//            title = LC_INDEX_MAIN_BEDROOM_LABEL;
//            break;
//        case ZYHomeRoomTypeLivingRoom:
//            title = LC_INDEX_MAIN_LIVING_ROOM_LABEL;
//            break;
//        case ZYHomeRoomTypeChildrenRoom:
//            title = LC_INDEX_MAIN_CHILDREN_ROOM_LABEL;
//            break;
//        default:
//            break;
//    }
    _roomTypeLabel.text = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:title];
    for (ZYIndexDeviceItemView *itemView in _deviceItemViewArray) {
        [itemView setupTitle];
    }
}

-(void)indexDeviceItemView:(ZYIndexDeviceItemView *)indexDeviceItemView didSelectDevice:(ZYDevice *)device{
    if([_delegate respondsToSelector:@selector(indexDeviceView:didSelectDevice:)]){
        [_delegate indexDeviceView:self didSelectDevice:device];
    }
}

@end
